package com.zlzxm.shoesmarket.view.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.android.material.tabs.TabLayout;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.activity.manage.ActivityIndex;
import com.zlzxm.shoesmarket.view.adapter.ADTransferGoods;
import com.zlzxm.shoesmarket.view.widget.MarketView;
import com.zlzxm.shoesmarket.view.widget.simpleead.SimpleHead;
import com.zlzxm.shoesmarket.viewmodel.VMTransferMarket;

/**
 * CREATE BY zlzxm ON 4/13/21
 */
public class ACTransferMarket extends BaseActivity implements View.OnClickListener, OnItemClickListener {


    public static void start(Context context){

        Intent intent = new Intent(context,ACTransferMarket.class);

        context.startActivity(intent);


    }

    private VMTransferMarket vmTransferMarket;

    private MarketView marketView;

    private RecyclerView rvGoods;

    private TabLayout tabLayout;

    private final ADTransferGoods adTransferGoods = new ADTransferGoods(null);

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.ac_transfer_market);
        SimpleHead simpleHead = ZViewHelp.findById(this,R.id.head);
        simpleHead.setTitle("调货市场");
        simpleHead.setOnItemclicklistener(this::finish);

        marketView = ZViewHelp.findById(this,R.id.marketView);
        tabLayout = ZViewHelp.findById(this,R.id.tabLayout);
        rvGoods = ZViewHelp.findById(this,R.id.rvGoods);
        rvGoods.setLayoutManager(new LinearLayoutManager(this));

        ZViewHelp.setOnClickListener(this,R.id.llMyRequest,this);

        adTransferGoods.setOnItemClickListener(this);
    }

    @Override
    protected void initViewModule() {
        super.initViewModule();
        vmTransferMarket = new ViewModelProvider(this).get(VMTransferMarket.class);

        vmTransferMarket.getLiveMarket().observe(this,marketView::updateMarkets);

        vmTransferMarket.getLiveTransferGood().observe(this, adTransferGoods::setList);
    }

    @Override
    protected void initData() {
        super.initData();
        rvGoods.setAdapter(adTransferGoods);

        tabLayout.addTab(tabLayout.newTab().setText("求货"));
        tabLayout.addTab(tabLayout.newTab().setText("出货"));
        tabLayout.addTab(tabLayout.newTab().setText("匹配求货"));
        tabLayout.addTab(tabLayout.newTab().setText("匹配出货"));
        tabLayout.addTab(tabLayout.newTab().setText("关注"));

        vmTransferMarket.requestMarkets();

        vmTransferMarket.requestTransferGood();

    }

    @Override
    public void onClick(View v) {


        if(v.getId() == R.id.llMyRequest){

            startACMyRequestGood();
        }


    }

    private void startACMyRequestGood(){

        ActivityIndex.startACMyRequestGood(this);
    }



    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

        ActivityIndex.startACGoodsMatchList(this,view.findViewById(R.id.ivFace),
                adTransferGoods.getData().get(position).getFaceUrl());


    }
}
